﻿using System;
using System.Web.UI;
using JQuery.WebKit.Base;

namespace JQuery.WebKit
{
    public class JCheckField : JControl
    {
        public JCheckField() : base("input") { }
        public JCheckField(string tag) : base("input") { }

        internal override PostType PostType { get { return Base.PostType.Value; } }

        public string ColumnName { get; set; }

        public override object Value
        {
            get
            {
                return base.Value != null ? base.Value : false;
            }
            set
            {
                base.Value = Convert.ToBoolean(value);
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            this.Attributes.Add("type", "checkbox");
            this.Attributes.Add("name", this.ClientID);
            this.Attributes.Add("style", "padding:0px; margin:0px");

            if (Convert.ToBoolean(Value))
            {
                this.Attributes.Add("checked", "true");
            }

            this.Attributes.Add("value", Convert.ToString(Value).ToLower());
            //this.Attributes.Add("onclick", "var checked = $(this).is(':checked');$(this).attr('value', checked);$(this).attr('jnamevalue', checked)");
            this.Attributes.Add("onclick", "var checked = $(this).is(':checked');$(this).attr('value', checked);");
            base.Render(writer);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.AddBodyScript(string.Format("$('#{0}').removeAttr('class');", this.ClientID));
            base.OnPreRender(e);
        }
    }
}
